package com.ryskoo.basicdata.area.city.repository;

import com.alibaba.fastjson.JSONArray;
import com.ryskoo.basicdata.area.city.domain.CityPO;
import com.ryskoo.common.constant.CacheConstant;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qyc
 */
@Primary
@Repository
public class CityRedisImpl extends BasicRedisImpl<CityPO> implements ICityRepository{
    @Autowired
    private RedisUtil redisClient;

    @Autowired
    private ICityRepository cityRepository;

    /**
     * 市缓存KEY
     */
    private static final String cacheKey = CacheConstant.BDS_AREA_CITY_KEY;
    /**
     * 市集合缓存KEY
     */
    private static final String cacheListKey = CacheConstant.BDS_AREA_CITY_LIST_KEY;
    /**
     * 省下的市集合缓存KEY
     */
    private static final String cacheCityProvinceKey = CacheConstant.BDS_AREA_CITY_PROVINCE_KEY;

    @PostConstruct
    public void init() {
        this.setRedisClient(redisClient);
        this.setDao(cityRepository);
    }

    public CityRedisImpl() {
        super(cacheKey);
    }

	@Override
	public List<CityPO> getAll() {
		//1.从缓存获取所有的地区-市
		Object object = redisClient.getSession(cacheListKey);
		if (object != null) {
            List<CityPO> cacheList = (List<CityPO>)object;
            return cacheList;
        }
		//2.缓存中如果没有取到，则从数据库中获取
		List<CityPO> dbList = super.getAll();
		
		if(!CollectionUtils.isEmpty(dbList)){
			//2.1 归类省市存放Map key=省编号，value=市集合
			Map<String,List<CityPO>> cityMap = new HashMap<String,List<CityPO>>();
			for(CityPO po : dbList){
				String key =po.getParentId();
				//判断当前key是否存在
				if(cityMap.containsKey(key)){
					List<CityPO> tempList = cityMap.get(key);
					if(CollectionUtils.isEmpty(tempList)){
						tempList = new ArrayList<CityPO>();
					}
					tempList.add(po);
				}else{
					List<CityPO> tempList = new ArrayList<CityPO>();
					tempList.add(po);
					cityMap.put(key, tempList);
				}
			}
			//2.2 把归类好的省市关系存放到缓存中
			if(!CollectionUtils.isEmpty(cityMap)){
				for(String key : cityMap.keySet()){
					List<CityPO> tempList = cityMap.get(key);
					redisClient.setObject(cacheCityProvinceKey + key, tempList);
				}
			}
		}
		//3.将结果放入缓存
        redisClient.setExpireSession(cacheListKey, dbList, null);

		return dbList;
	}

	@Override
	public List<CityPO> getCitysByProvinceId(String provinceId) {
		//1.根据key从缓存获取所有的地区-市
		/*Object object = redisClient.getObject(cacheCityProvinceKey+provinceId, List.class);
		if (object != null) {
            List<CityPO> cacheList = (List<CityPO>)object;
            return cacheList;
        }*/
		String jsonStr = redisClient.getString(cacheCityProvinceKey+provinceId);
		if(!StringUtils.isEmpty(jsonStr)){
			List<CityPO> cacheList = JSONArray.parseArray(jsonStr,CityPO.class);
			return cacheList;
		}
		//2.缓存中如果没有取到，则从数据库中获取
		List<CityPO> dbList = cityRepository.getCitysByProvinceId(provinceId);
		//3.将结果放入缓存
        redisClient.setObject(cacheCityProvinceKey+provinceId, JSONArray.toJSONString(dbList));
		return dbList;
	}

	@Override
	public CityPO getByEntityNo(String entityNo) {
		return cityRepository.getByEntityNo(entityNo);
	}
}
